package com.nut.controller;

import com.nut.entity.Article;
import com.nut.entity.User;
import com.nut.service.ArticleService;
import com.nut.service.UserService;
import com.nut.vo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-04-20 20:11
 * @ Description : User controller层
 */

@Controller
@CrossOrigin
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;

    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Message getUserById(@RequestParam("id") int id)
    {
        //查询User
        User user = userService.findUserById(id);

        //查询User的文章
        user.setArticles(articleService.findAllArticles(user));

        return Message.success().payLoad("user",user);
    }


    @ResponseBody
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Message getAllUsers()
    {
        List<User> users;
        users = userService.findAllUsers();
        return Message.success().payLoad("users",users);
    }


    @ResponseBody
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public Message saveOrUpdateUser(@RequestBody User user)
    {
        try
        {
            if(user.getId()==0)
            {
                userService.saveUser(user);
                return Message.success().payLoad("message","添加用户成功！");
            }
            else
            {
                userService.updateUser(user);
                return Message.success().payLoad("message","修改用户成功！");
            }

        }
        catch (Exception e)
        {
            return Message.fail().payLoad("message","添加用户失败！");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public Message deleteUserById(@RequestParam("id") int id)
    {
        try
        {
            userService.deleteUserById(id);
            return Message.success().payLoad("message","删除用户成功！");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Message.fail().payLoad("message","删除用户失败！");
        }
    }

    @ResponseBody
    @RequestMapping("/findByPage")
    public Message findByPage(Integer pageNow,Integer pageSize)
    {
        pageNow = pageNow == null? 1 : pageNow;
        pageSize = pageSize == null? 5 : pageSize;
        Map<String,Object> results = new HashMap<>();
        List<User> users = userService.findByPage(pageNow, pageSize);
        Long totals = userService.findTotals();
        results.put("users",users);
        results.put("totals",totals);

        return Message.success().payLoad("results",results);

    }

}
